package com.socket.socketAsk;

import java.io.IOException;
import java.net.*;

/**
 * AskClient--bdqnPractice
 *
 * @author: Jovi
 * @createTime: 2017-09-08 10:35
 **/
public class AskClient {

    public static void main(String[] args) {

        String mess = "你好，我想咨询问题";
        byte[] buf = mess.getBytes();
        InetAddress ia = null;
        try {
            ia = InetAddress.getByName("localhost");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        int port = 8800;
        DatagramPacket dp = new DatagramPacket(buf, buf.length, ia, port);
        try {
            DatagramSocket ds = new DatagramSocket();
            ds.send(dp);

            byte[] receive = new byte[1024];
            DatagramPacket dp2 = new DatagramPacket(receive, receive.length);
            ds.receive(dp2);
            String reply = new String(receive,0,dp2.getLength());
            System.out.println("服务器响应为："+reply);

            ds.close();

        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
